package com.deng.leaf.core.segment.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.deng.leaf.core.segment.model.LeafAlloc;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface IDAllocMapper extends BaseMapper<LeafAlloc> {

    @Select("SELECT biz_tag, max_id, step, update_time FROM leaf_alloc")
    @Results(value = {
            @Result(column = "biz_tag", property = "key"),
            @Result(column = "max_id", property = "maxId"),
            @Result(column = "step", property = "step"),
            @Result(column = "update_time", property = "updateTime")
    })
    List<LeafAlloc> getAllLeafAllocs();

    @Select("SELECT biz_tag, max_id, step FROM leaf_alloc WHERE biz_tag = #{tag}")
    @Results(value = {
            @Result(column = "biz_tag", property = "key"),
            @Result(column = "max_id", property = "maxId"),
            @Result(column = "step", property = "step")
    })
    LeafAlloc getLeafAlloc(@Param("tag") String tag);

    @Update("UPDATE leaf_alloc SET max_id = max_id + step WHERE biz_tag = #{tag}")
    int updateMaxId(@Param("tag") String tag);

    @Update("UPDATE leaf_alloc SET max_id = max_id + #{step} WHERE biz_tag = #{key}")
    int updateMaxIdByCustomStep(@Param("leafAlloc") LeafAlloc leafAlloc);


    @Select("SELECT biz_tag, max_id, step, init_value, max_value FROM leaf_alloc WHERE biz_tag = #{tag}")
    @Results(value = {
            @Result(column = "biz_tag", property = "key"),
            @Result(column = "max_id", property = "maxId"),
            @Result(column = "step", property = "step"),
            @Result(column = "init_value", property = "initValue"),
            @Result(column = "max_value", property = "maxValue")
    })
    LeafAlloc getLeafByTag(@Param("tag") String tag);

    @Update("UPDATE leaf_alloc SET max_id = init_value WHERE biz_tag = #{tag}")
    int updateMaxIdEqInitValueByTag(@Param("tag") String tag);

    @Select("SELECT biz_tag FROM leaf_alloc")
    List<String> getAllTags();

}
